package com.ruoyi.inspection.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.inspection.domain.GovernCheckTaskItem;
import com.ruoyi.inspection.mapper.GovernCheckTaskItemMapper;
import com.ruoyi.inspection.service.GovernCheckTaskItemService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 政府下发专项任务检查项表(GovernCheckTaskItem)表服务实现类
 *
 * @author makejava
 * @since 2024-06-18 10:48:51
 */
@Service("governCheckTaskItemService")
public class GovernCheckTaskItemServiceImpl implements GovernCheckTaskItemService {
    @Resource
    private GovernCheckTaskItemMapper governCheckTaskItemMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public GovernCheckTaskItem queryById(Long id) {
        GovernCheckTaskItem entity = this.governCheckTaskItemMapper.queryById(id);
        if (StringUtils.isNotEmpty(entity.getCheckTaskType())) {
            entity.setCheckTaskTypes(Arrays.asList(entity.getCheckTaskType().split(",")));
        }
        return entity;
    }

    /**
     * 分页查询
     *
     * @param governCheckTaskItem 筛选条件
     * @return 查询结果
     */
    @Override
    public List<GovernCheckTaskItem> queryByPage(GovernCheckTaskItem governCheckTaskItem) {
        return this.governCheckTaskItemMapper.queryAllByLimit(governCheckTaskItem);
    }

    /**
     * 检查类型
     * @return 查询结果
     */
    @Override
    public List<String> getCheckTaskTypes() {
        return this.governCheckTaskItemMapper.getCheckTaskTypes();
    }

    /**
     * 新增数据
     *
     * @param governCheckTaskItem 实例对象
     * @return 实例对象
     */
    @Override
    public GovernCheckTaskItem insert(GovernCheckTaskItem governCheckTaskItem) {
        governCheckTaskItem.setGovernCreateDate(new Date());
//        List<String> checkTaskTypes = governCheckTaskItem.getCheckTaskTypes();
//        if (StringUtils.isNotEmpty(checkTaskTypes)) {
//            governCheckTaskItem.setCheckTaskType(String.join(",", checkTaskTypes));
//        }
        this.governCheckTaskItemMapper.insert(governCheckTaskItem);
        return governCheckTaskItem;
    }

    /**
     * 修改数据
     *
     * @param governCheckTaskItem 实例对象
     * @return 实例对象
     */
    @Override
    public GovernCheckTaskItem update(GovernCheckTaskItem governCheckTaskItem) {
        governCheckTaskItem.setGovernUpdateDate(new Date());
//        List<String> checkTaskTypes = governCheckTaskItem.getCheckTaskTypes();
//        if (StringUtils.isNotEmpty(checkTaskTypes)) {
//            governCheckTaskItem.setCheckTaskType(String.join(",", checkTaskTypes));
//        }
        this.governCheckTaskItemMapper.update(governCheckTaskItem);
        return this.queryById(governCheckTaskItem.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.governCheckTaskItemMapper.deleteById(id) > 0;
    }
}
